/**
 * @description: 节流函数
 * @param {func} 需要节流的函数
 * @param {wait} 节流时间区
 * @return {*} (context,...args):void
 */
function throttle(func, limit) {
    let lastFunc;
    let lastRan;
    return function (...args) {
        const now = Date.now();
        const context = this; // 保存当前的 this 上下文
        if (!lastRan) {
            func.apply(context, args);
            lastRan = now;
        }
        else {
            clearTimeout(lastFunc);
            lastFunc = setTimeout(() => {
                if (now - lastRan >= limit) {
                    func.apply(context, args);
                    lastRan = now;
                }
            }, limit - (now - lastRan));
        }
    };
}

export { throttle };
